package com.gisuni.sddc.platformManager.controller;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.json.JSONArray;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import com.gisuni.sddc.platformManager.model.Response;
import com.gisuni.sddc.platformManager.model.RoleInfo;
import com.gisuni.sddc.platformManager.service.ProjectTypeService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * @ClassName:  ProjectTypeController   
 * @Description: 项目类型的增删改   
 * @author: landa
 * @date:   2017年3月16日 下午2:17:55   
 *
 */
@IocBean(singleton = false)
@At("/client/backgroundManagement/projectTypeController")
public class ProjectTypeController   {

	private Log log = Logs.getLog(ProjectTypeController.class);
 
	@Inject
    private ProjectTypeService projectTypeService;

 
	@At("/dataGrid")
	@GET
	@POST
	@Ok("raw:json") 
    public Object dataGrid(@Param("name") String typeName,@Param("area") String area,@Param("page") int page, @Param("rows") int rows,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			int from=(page-1)*rows;
			res = projectTypeService.queryBaseEntityAll(page,rows,typeName,area);
			r = Response.toJSONObject(res);
		} catch (Exception e) {
			log.error(e.getMessage());
			res.setMsg(e.getMessage());
			return res;
		}
		return r;
    }

 
	@At("/tree")
	@GET
	@POST
	@Ok("json") 
    public Object tree(@Param("flag") String flag) {
        return projectTypeService.findTree(flag);
    }
    

	@At("/add")
	@GET
	@POST
	@Ok("json")
    public Object add(HttpServletRequest request) {
       
		Response res = new Response();
		res.setSuccess(false);
		try {
			res =   projectTypeService.addEntity(request);//返回格式:{}
		} catch (Exception e) {
			log.error(e.getMessage());
			res.setMsg("操作失败,请稍后重试");
		}
		return res;
    }
    
	@At("/edit")
	@GET
	@POST
	@Ok("json")
    public Object edit(HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			res = projectTypeService.updateEntity(request); 
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
    }
	

	@At("/delete")
	@GET
	@POST
	@Ok("json")
    public Object delete(String id) {
		Response res = new Response();
		res.setSuccess(false);
		try {
			res =  projectTypeService.deleteEntityById(id);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
    }

    
	@At("/editPage")
	@GET
	@POST
	@Ok("jsp:/client/backgroundManagement/views/admin/projectTypeEdit.jsp")
    public String editPage( HttpServletRequest request, String id) {
        String result = projectTypeService.findEntityById(id);
        JSONArray arr = new JSONArray(result);
        request.setAttribute("entity", arr);
        return null;
    }

    
 
   	@At("/findResourceIdListByRoleId")
   	@GET
   	@POST
   	@Ok("json")
    public Object findResourceByRoleId(Long id) {
//        List<Long> resources = roleService.findResourceIdListByRoleId(id);
//        return renderSuccess(resources);
        return null;
    }
   	
   	/**
   	 * 获取所有类型的时间节点
   	 * @return
   	 */
    @At("/getAllTypeTimeNode")
    @GET
    @POST
    @Ok("json")
    public String getAllTypeTimeNode(){
    	String res = new String();
		try {
			res =  projectTypeService.getAllTypeTimeNode();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
    }
   	@At("/getTimeNodeByTypeId/")
   	@GET
   	@POST
   	@Ok("json")
   	public String getTimeNodeByTypeId(@Param(value="typeId") String id){
   		String res = new String();
		try {
			res =  projectTypeService.getTimeNodeByTypeId(id);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
   		
   	}
   	@At("/getZCQXMBelongInfo/")
   	@POST
   	@GET
   	@Ok("json")
   	public String getZCQXMBelongInfo(@Param(value="table")String table){
   		String res = new String();
		try {
			res =  projectTypeService.getZCQXMBelongInfo(table);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
   		
   	}

}
